package com.fg.pro.trade.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fg.pro.model.trade.dto.GmSupplierDto;
import com.fg.pro.trade.dao.TradeSupplierDao;
import com.fg.pro.trade.entity.TradeCustomer;
import com.fg.pro.trade.entity.TradeSupplier;
import com.fg.pro.trade.service.TradeSupplierService;
import com.fg.pro.trade.utils.GmHttpUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.fg.pro.trade.utils.GmRequestUrl.*;

/**
 * 供应商(TradeSupplier)表服务实现类
 *
 * @author makejava
 * @since 2025-08-25 10:40:54
 */
@Service("tradeSupplierService")
public class TradeSupplierServiceImpl extends ServiceImpl<TradeSupplierDao, TradeSupplier> implements TradeSupplierService {

    @Autowired
    GmHttpUtils gmHttpUtils;
    @Autowired
    RedissonClient redissonClient;

    @Override
    public PageInfo getPage(Integer pageNum, Integer pageSize, String supplierName) {
        PageHelper.startPage(pageNum, pageSize);
        List<TradeSupplier> list = query().like(StrUtil.isNotBlank(supplierName),"supplier_name",supplierName).list();
        PageInfo<TradeSupplier> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean refresh(Date date) {
        RLock lock = redissonClient.getLock(REDIS_LOCK_URL+GM_CUSTOMER_URL);
        boolean tryLock = lock.tryLock();
        if(!tryLock){
            return false;
        }
        try {
            return refreshTradeSupplierByLock(date);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    private boolean refreshTradeSupplierByLock(Date date) throws InterruptedException {
        GmSupplierDto.GmSupplierReponse gmSupplierReponse = gmHttpUtils.httpPostByToken(GM_SPULLIER_URL,new GmSupplierDto().new GmSupplierRequest().gmSupplierRequest(999), GmSupplierDto.GmSupplierReponse.class);
        LinkedList<TradeSupplier> newList = new LinkedList<>();
        for (GmSupplierDto.GmSupplierReponse.GmSupplierData gmSupplierData : gmSupplierReponse.getSuppliers()) {
            TradeSupplier tradeSupplier = new TradeSupplier();
            tradeSupplier.setId(gmSupplierData.supplier_id);
            tradeSupplier.setCreateTime(new Date(gmSupplierData.getCreate_time()));
            tradeSupplier.setUpdateTime(new Date(gmSupplierData.getUpdate_time()));
            tradeSupplier.setSupplierGroupId(gmSupplierData.getGroup_id());
            tradeSupplier.setSupplierName(gmSupplierData.getName());
            tradeSupplier.setSupplierCreditType(gmSupplierData.getCredit_type());
            tradeSupplier.setSupplierCustomizedCode(gmSupplierData.getCustomized_code());
            tradeSupplier.setSupplierInvoiceType(gmSupplierData.getAttrs().getChina_vat_invoice().invoice_type);
            tradeSupplier.setSupplierBankName(gmSupplierData.getAttrs().getChina_vat_invoice().getBank_name());
            tradeSupplier.setSupplierBankAccount(gmSupplierData.getAttrs().getChina_vat_invoice().getBank_account());
            tradeSupplier.setWarehouseId(gmSupplierData.getWarehouse_id());
            tradeSupplier.setSupplierType(gmSupplierData.getSupplier_type());
            newList.add(tradeSupplier);
        }
        boolean saved = saveOrUpdateBatch(newList);
        return saved;
    }
}

